package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * @Description: hamo
 * @Create by: 渭北彭于晏
 * @Date: 2021/10/29 20:54
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService service;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //接收前端传递的用户名和密码
        String name = employee.getUsername();
        //根据用户查询员工对象
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,name);
        Employee ey = service.getOne(wrapper);
        //判断员工对象是否为null
        if (ey == null){
        //若为null,给前端响应用户名不存在
            return R.error("用户名不存在");
        }
        //如果不为null:检验密码
        //获取前端传递的明文密码
        String str = employee.getPassword();
        //对密码进行md5加密
        String password = DigestUtils.md5DigestAsHex(str.getBytes());
        //使用加密后的密文和数据库的密文进行对比
        if (!ey.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if (ey.getStatus() == 0){
            return R.error("账户已被禁用");
        }else {
            request.getSession().setAttribute("employee",ey.getId());
            return R.success(ey);
        }
    }
    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R add(HttpServletRequest request,@RequestBody Employee employee){
        //获取前端传来的的数据并补齐密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
       /* employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //获得当前登录用户的id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/
        //调用service方法完成添加
        service.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 分页查询员工信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize,String name){

        IPage<Employee> pageInfor = new Page<>(page,pageSize);

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(name)){
            wrapper.like("name",name);
        }

        wrapper.orderByDesc("create_time");

        IPage<Employee> result = service.page(pageInfor,wrapper);

        return R.success(result);
    }

    /**
     * 修改状态
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R updateById(@RequestBody Employee employee, HttpSession session){

      /*  Long userid = (Long) session.getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(userid);*/

        service.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R update(@PathVariable("id") Long id){
        Employee emp = service.getById(id);
        return R.success(emp);
    }
}
